package com.lj.back.manage.scaffold.module.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.common.valid.group.AddStrategy;
import com.lj.back.manage.scaffold.common.valid.group.UpdateStrategy;
import com.lj.back.manage.scaffold.module.sys.DTO.SysMenuDTO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysMenuPageQuery;
import com.lj.back.manage.scaffold.module.sys.VO.SysMenuVO;
import com.lj.back.manage.scaffold.module.sys.annotation.SysLog;
import com.lj.back.manage.scaffold.module.sys.enums.OperateTypeEnum;
import com.lj.back.manage.scaffold.module.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;
import java.util.stream.Collectors;

/**
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "后台管理---菜单模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @GetMapping("/list")
    @ApiOperation(value = "分页获取菜单列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:menu:list")
    public R listMenu(@Validated SysMenuPageQuery pageQuery){
        IPage<SysMenuDTO> sysMenuDTOPage =  sysMenuService.listMenus(pageQuery);
        IPage<SysMenuVO> sysMenuVOPage =  CommonUtil.pageBeanConvert(sysMenuDTOPage, SysMenuVO.class);
        return R.success(sysMenuVOPage);
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取全部菜单列表")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:menu:list")
    public R listMenu(){
        LinkedHashSet<SysMenuDTO> sysMenuDTOS =  sysMenuService.listMenus();
        if (CollectionUtil.isEmpty(sysMenuDTOS)){
            return R.success();
        }
        LinkedHashSet<SysMenuVO> sysMenuVOS = sysMenuDTOS.stream().map(sysMenuDTO -> BeanUtil.copyProperties(sysMenuDTO,SysMenuVO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
        return R.success(sysMenuVOS);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID获取菜单详情")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:menu:select")
    @ApiImplicitParam(name = "id", value = "菜单ID", example = "1", required = true, dataTypeClass = Long.class)
    public R listMenu(@PathVariable Long id){
        SysMenuDTO sysMenuDTO = sysMenuService.getMenuInfo(id);
        SysMenuVO sysMenuVO = BeanUtil.copyProperties(sysMenuDTO,SysMenuVO.class);
        return R.success(sysMenuVO);
    }

    @PostMapping("/add")
    @SysLog(value = "新增菜单", operateType = OperateTypeEnum.INSERT)
    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(order = 4,ignoreParameters={"createTime","id"})
    @RequiresPermissions("sys:menu:insert")
    public R addMenu(@RequestBody @Validated(AddStrategy.class) SysMenuDTO sysMenuDTO){
        long menuId = sysMenuService.addMenu(sysMenuDTO);
        return R.build("id", menuId);
    }

    @DeleteMapping("/delete/{id}")
    @SysLog(value = "删除菜单", operateType = OperateTypeEnum.DELETE)
    @ApiOperation(value = "删除菜单")
    @ApiOperationSupport(order = 5)
    @RequiresPermissions("sys:menu:delete")
    @ApiImplicitParam(name = "id", value = "菜单ID", example = "1", required = true, dataTypeClass = Long.class)
    public R deleteRole(@PathVariable Long id){
        boolean isSuccess = sysMenuService.removeMenu(id);
        return R.build(isSuccess);
    }

    @PatchMapping("/update")
    @SysLog(value = "更新菜单", operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "更新菜单")
    @ApiOperationSupport(order = 6,ignoreParameters={"createTime","name","type","parentId"})
    @RequiresPermissions("sys:menu:update")
    public R updateRole(@RequestBody @Validated(UpdateStrategy.class) SysMenuDTO sysMenuDTO){
        boolean isSuccess = sysMenuService.updateMenu(sysMenuDTO);
        return R.build(isSuccess);
    }
}
